require.config({
    baseUrl: '../javascripts/library',
    paths: {
        jquery: 'jquery',
        cookie: 'cookie',
    },

});
require(['jquery', 'axios'], function($, axios) {

    let shop = cookie.get("shop");
    if (shop) {
        shop = JSON.parse(shop);
        let idList = shop.map(el => el.id).join();
        $.ajax({
            type: "get",
            url: "../../interface/getItems.php",
            data: {
                idList
            },
            dataType: "json"
        }).then(function(res) {
            let temp = '';
            var arr = [];
            var count = 0;
            res.forEach(el => {

                let picture = JSON.parse(el.picture);
                let current = shop.filter(elm => elm.id === el.id);

                temp += `
                
                    <div class="sc-pro clear">
                        <label>
                            <input type="checkbox">
                        </label>
                        <div class="sc-pro-area">
                            <div class="in clear">
                                <a href="">
                                    <img src="${picture[0].src}" alt="">
                                </a>
                                <ul class="clear">
                                    <li>
                                        <a href="">     
                                            ${el.title}
                                        </a>
                                        <p>白色</p>
                                        <p>此商品由广州禾宇网络科技有限公司发货</p>
                                    </li>
                                    <li>
                                        <p class="new">
                                            ¥ ${parseFloat(el.price).toFixed(2)}
                                        </p>
                                        <p class="old">
                                            ¥ 599.00
                                        </p>
                                    </li>
                                    <li>
                                        <div>
                                            <input type="number" value="${current[0].num}" min="1">
                                            <p class="clear">
                                                <a href="javascript:;" class="js">
                                                    -
                                                </a>
                                                <a href="javascript:;" class="zj">
                                                    +
                                                </a>
                                            </p>
                                        </div>
                                    </li>
                                    <li>
                                        <p>¥ ${(current[0].num * parseFloat(el.price)).toFixed(2)}</p>
                                        <p>省¥ 150.00</p>
                                    </li>
                                    <li>
                                        <a href="javascript:;" class="remove" data-id="${el.id}">删除</a>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                
                `;


                arr.push(current);
            });
            $('form').html(temp).find('.remove').on('click', function() {
                let res = shop.filter(el => el.id !== $(this).attr('data-id'));
                cookie.set('shop', JSON.stringify(res), 1);
                location.reload();
            });
            let em = $('.main .wrapper .sc-list .b ul li:nth-of-type(3) p:nth-of-type(2) em');
            let p = $('.wrapper .sc-list form .sc-pro .sc-pro-area .in > ul li:nth-of-type(4) p:nth-of-type(1)');
            var span = $('.wrapper .sc-list .b ul li:nth-of-type(3) p:nth-of-type(1) span');
            let input = $('.main .wrapper .sc-list form .sc-pro .sc-pro-area .in > ul li:nth-of-type(3) div input');
            for (let i = 0; i < shop.length; i++) {
                let sum = arr[i][0].price * arr[i][0].num;
                $(p[i]).text('¥ ' + sum);
                count += sum;
            }
            let li = $('.main .wrapper .sc-list form .sc-pro .sc-pro-area .in > ul li:nth-of-type(3)');
            em.text(arr.length);
            span.text(count);
            li.on('click', function(event) {

                if ($(event.target).attr("class") === "zj") {
                    $(event.target.parentNode).siblings().val(function() {
                        return parseInt(this.value) + 1;
                    });
                }
                if ($(event.target).attr("class") === "js") {

                    if ($(this.children).children('input').val() <= 1) {
                        $(event.target.parentNode).siblings().val(function() {
                            return 1;
                        });
                    } else {
                        $(event.target.parentNode).siblings().val(function() {
                            return parseInt(this.value) - 1;
                        });
                    }
                }
                let id1 = $(this).next().next().children('a').attr("data-id");
                let val1 = $(this.children).children('input').val();
                for (var key in shop) {

                    if (shop[key].id === id1) {
                        shop[key].num = val1;
                    }
                }
                let string1 = $(this).prev().children('.new').text();
                string1 = $.trim(string1);
                string1 = string1.slice(2);
                $(this).next().children('p:eq(0)').text('¥ ' + string1 * val1);

                let price = $('.main .wrapper .sc-list form .sc-pro .sc-pro-area .in > ul li:nth-of-type(4) p:nth-of-type(1)');

                let count2 = 0
                for (let i = 0; i < price.length; i++) {
                    count2 += parseInt($(price[i]).text().slice(2));

                }

                span.text(count2);


                cookie.set('shop', JSON.stringify(shop), 1);
            });
        }).catch(function(xhr) {
            console.log(xhr.status);
        })
    }
});